﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;
using YJKRevitView;
using YJKCopyModel.Forms.RoomTransModelForms;
using YJKCopyModel.InteractiveOperation;

namespace YJKCopyModel.RoomTransModel
{
	public class RoomRequestMaker : InteractiveOperationBase
	{
		public RoomRequestMaker(ExternalCommandData useExternalCommandData) : base(useExternalCommandData)
		{
			this.m_doc = useExternalCommandData.Application.ActiveUIDocument.Document;
		}

		public override bool IfBreak
		{
			get
			{
				throw new NotImplementedException();
			}
		}

		public override void CloseFrom()
		{
			throw new NotImplementedException();
		}

		public override List<InputRequest> GetAllInputRequest()
		{
			List<InputRequest> list = new List<InputRequest>();
			foreach (RoomRequest item in this.m_lstRoomRequest)
			{
				list.Add(item);
			}
			return list;
		}

		public override void PrepareRequest()
		{
			CmdViewType types = (CmdViewType)13;
			bool flag = SwitchView.SetUsableView(types, this.m_useExternalCommandData) > 0;
			if (flag)
			{
				this.m_bCancel = true;
			}
			else
			{
				List<TextInfo> lstText = null;
				bool flag2 = false;
				this.SelectRoomName(out lstText, out flag2);
				bool flag3 = flag2;
				if (flag3)
				{
					this.RecoverLayer();
					this.m_bCancel = true;
				}
				else
				{
					Dictionary<RoomConfigInfo, List<TextInfo>> dicConfigAndText = RoomManager.GroupRoom(lstText);
					StatisticsForm statisticsForm = new StatisticsForm(this.m_doc, dicConfigAndText);
					bool flag4 = statisticsForm.ShowDialog() != DialogResult.OK;
					if (flag4)
					{
						this.m_bCancel = true;
					}
					else
					{
						this.CreateRoomRequest(statisticsForm.ConfigAndText);
					}
				}
			}
		}

		private void SelectRoomName(out List<TextInfo> lstSelected, out bool bExitCommand)
		{
			lstSelected = new List<TextInfo>();
			bExitCommand = false;
			List<TextInfo> list = new List<TextInfo>();
			try
			{
				MainForm mainForm = new MainForm();
				DialogResult dialogResult = DialogResult.OK;
				while ((dialogResult = mainForm.ShowDialog(this.m_handleRevit)) == DialogResult.OK)
				{
					bool flag = false;
					switch (mainForm.CloseDlgType)
					{
					case CloseDlgType.RT_Otheer:
						flag = true;
						bExitCommand = true;
						break;
					case CloseDlgType.RT_SetupParam:
					{
						SetUpForm setUpForm = new SetUpForm();
						setUpForm.ShowDialog(mainForm);
						break;
					}
					case CloseDlgType.RT_SelectRoomName:
					{
						List<TextInfo> list2 = null;
						Transaction transaction = new Transaction(this.m_doc, "隐藏选择图元的图层");
						try
						{
							transaction.Start();
							this.m_dwgEntitiesSelection.TryPickSameLayerText(out list2, true, "请选择链接dwg图中的房间名称文字。");
							transaction.Commit();
							bool flag2 = this.m_dwgEntitiesSelection.Result > DwgEntitiesSelectionResult.eOK;
							if (flag2)
							{
								bool flag3 = false;
								string resultInfo = DwgEntitiesSelectionCommon.GetResultInfo(this.m_dwgEntitiesSelection.Result, out flag3);
								bool flag4 = !flag3;
								if (flag4)
								{
									YJKMessageBox.Show(resultInfo);
								}
							}
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
							transaction.RollBack();
							continue;
						}
						catch (Exception)
						{
							bool flag5 = transaction.HasStarted();
							if (flag5)
							{
								transaction.RollBack();
							}
						}
						bool flag6 = !list2.IsEmpty<TextInfo>();
						if (flag6)
						{
							this.m_lstHideLayer.Add(list2[0].RevitLayer);
							mainForm.SelectedLayer = (from item in this.m_lstHideLayer
							select item.Name).ToList<string>();
						}
						list.AddRange(list2);
						break;
					}
					case CloseDlgType.RT_Clear:
						this.RecoverLayer();
						list.Clear();
						this.m_lstHideLayer.Clear();
						mainForm.SelectedLayer.Clear();
						break;
					case CloseDlgType.RT_TransForLevel:
						this.RecoverLayer();
						lstSelected.AddRange(list);
						flag = true;
						break;
					case CloseDlgType.RT_TransForLocal:
					{
						this.RecoverLayer();
						List<TextInfo> list3 = new List<TextInfo>();
						try
						{
							for (;;)
							{
								this.m_dwgEntitiesSelection.TryGetEntitesInPickBox<TextInfo>(out list3, list, delegate(TextInfo txt, PickedBox pickBox)
								{
									XYZ xyz = (txt.ActualBox[0] + txt.ActualBox[3]) / 2.0;
									XYZ xyz2 = new XYZ(Math.Min(pickBox.Min.X, pickBox.Max.X), Math.Min(pickBox.Min.Y, pickBox.Max.Y), 0.0);
									XYZ xyz3 = new XYZ(Math.Max(pickBox.Min.X, pickBox.Max.X), Math.Max(pickBox.Min.Y, pickBox.Max.Y), 0.0);
									return xyz.X > xyz2.X && xyz.Y > xyz2.Y && xyz.X < xyz3.X && xyz.Y < xyz3.Y;
								}, "请框选要识别的范围。");
								bool flag7 = list3.IsEmpty<TextInfo>();
								if (!flag7)
								{
									break;
								}
								YJKMessageBox.Show("范围内未能获取到房间名，请重新框选要识别的范围。");
							}
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
							continue;
						}
						lstSelected.AddRange(list3);
						flag = true;
						break;
					}
					default:
						flag = true;
						bExitCommand = true;
						break;
					}
					bool flag8 = flag;
					if (flag8)
					{
						break;
					}
				}
				bool flag9 = dialogResult != DialogResult.OK;
				if (flag9)
				{
					bExitCommand = true;
				}
			}
			catch (Exception)
			{
				bExitCommand = true;
			}
		}

		private void CreateRoomRequest(Dictionary<RoomConfigInfo, List<TextInfo>> dicConfigAndTexts)
		{
			RoomRequest roomRequest = new RoomRequest();
			bool flag = roomRequest.ConfigAndTexts == null;
			if (flag)
			{
				roomRequest.ConfigAndTexts = new Dictionary<RoomConfigInfo, List<TextInfo>>();
			}
			bool flag2 = dicConfigAndTexts.IsEmpty<KeyValuePair<RoomConfigInfo, List<TextInfo>>>();
			if (!flag2)
			{
				RoomTagType thisRoomTagType = RoomManager.CreateRoomTagType(this.m_doc, dicConfigAndTexts.ElementAt(0).Key.RoomTypeName);
				foreach (KeyValuePair<RoomConfigInfo, List<TextInfo>> keyValuePair in dicConfigAndTexts)
				{
					RoomConfigInfo key = keyValuePair.Key;
					List<TextInfo> value = keyValuePair.Value;
					bool flag3 = !keyValuePair.Key.Rebuild;
					if (!flag3)
					{
						key.ThisRoomTagType = thisRoomTagType;
						roomRequest.ConfigAndTexts.Add(key, value);
					}
				}
				roomRequest.UseExternalCommandData = this.m_useExternalCommandData;
				this.m_lstRoomRequest.Add(roomRequest);
			}
		}

		private void RecoverLayer()
		{
			Transaction transaction = new Transaction(this.m_doc, "恢复隐藏的图层");
			try
			{
				transaction.Start();
				foreach (Category category in this.m_lstHideLayer)
				{
					this.m_doc.ActiveView.HYSetVisibility(category, true);
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				bool flag = transaction.HasStarted();
				if (flag)
				{
					transaction.RollBack();
				}
			}
		}

		private List<RoomRequest> m_lstRoomRequest = new List<RoomRequest>();

		private List<Category> m_lstHideLayer = new List<Category>();

		private Document m_doc;
	}
}
